package com.ruoyi.web.controller.kpl.other;

import java.io.*;

public class FileUtils {
    /**
     *
     * @param filePath
     * @return
     */
    public static String read(String filePath) {
        try{
            File file = new File(filePath);
            if(!file.exists()){
                return null;
            }
            BufferedReader br = new BufferedReader(new FileReader(filePath));
            String line = null;
            String data = "";
            while ((line = br.readLine()) != null){
                data = data + line;
            }
            br.close();
            return data;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /**
     *
     * @param filePath 文件路径
     * @param content 文件内容
     * @param append 是否追加
     * @param delete 是否删除源文件
     */
    public static void write(String filePath,String content,boolean append){
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter(filePath,append));

            bw.write(content);
            bw.write("\n");

            bw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void write(String folder,String fileName,String content){

        try{
            File file = new File(folder);

            if(!file.exists()){
                file.mkdirs();
            }

            BufferedWriter bw = new BufferedWriter(new FileWriter(folder+"/"+fileName));
            bw.write(content);
            bw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
